package com.blinddate.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blinddate.mapper.UserInfMapper;
import com.blinddate.mapper.UserMapper;
import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.result.Result;
import com.blinddate.properties.AliOssProperties;
import com.blinddate.utils.AliOssUtil;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

/**
 * 文件上传下载
 */
@RestController
@RequestMapping("/common")
@Slf4j
@Api(tags = "通用功能")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public Result<String> upload(MultipartFile file){//进行文件传输
       log.info(file.toString());

       //动态获取文件后缀
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //根据uuid设置文件名
        String s = UUID.randomUUID().toString()+substring;
        try {
            String filePath = aliOssUtil.upload(file.getBytes(), s);
            return Result.success(filePath);
        } catch (IOException e) {
            throw new RuntimeException("上传文件模块有异常请查看CommonController类:"+e.getMessage());
        }
//        文件名
    }

//    @GetMapping("/download")
//    public void download(String name, HttpServletResponse response){
//        //读取文件
//        try {
//            FileInputStream fileInputStream=new FileInputStream(basePath+name);
//            ServletOutputStream outputStream = response.getOutputStream(); //输出文件
//            response.setContentType("imag/jpeg");
//            int len=0;
//            byte[] bytes = new byte[1024];
//            while ((len=fileInputStream.read(bytes))!=-1){ //进行读取read返回的是读取到的多少，没有读取则返回-1
//                outputStream.write(bytes,0,len);
//                outputStream.flush();//刷新
//            }
//            outputStream.close();
//            fileInputStream.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //输出文件内容
//    }
    @GetMapping("/pdf")
    @ApiOperation("获取用户的pdf信息" )
    public Result<String> pdf(Long userid) throws IOException {
        User user=userMapper.selectById(userid);
        if(user.getPdfPath()!=null){
            return Result.success(user.getPdfPath());
        }else {
            LambdaQueryWrapper<UserInf> lqw=new LambdaQueryWrapper<UserInf>();
            lqw.eq(UserInf::getUserId,userid);
            UserInf userInf=userInfMapper.selectOne(lqw);
            String s = UUID.randomUUID().toString()+".pdf";
            String pdfpath=aliOssUtil.pdf(userInf,s);
            user.setPdfPath(pdfpath);
            userMapper.updateById(user);
            return Result.success(pdfpath);
        }

    }

}

